package com.cugmarket.controller.user;

import static com.cugmarket.constant.MessageConstant.*;

import com.cugmarket.constant.MessageConstant;
import com.cugmarket.dto.LoginFormDTO;
import com.cugmarket.dto.Result;
import com.cugmarket.dto.UserDTO;
import com.cugmarket.service.UserService;
import com.cugmarket.utils.IpAddressUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    private final HttpServletRequest request;

    public UserController(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * 发送手机验证码
     */
    @PostMapping("/sendCode")
    public Result<String> sendCode(@RequestParam("phone") String phone) {
        String code = userService.sendCode(phone);
        return Result.success(code);
    }

    @GetMapping("/is_login")
    public Result<String> isLogin() {
        return Result.success();
    }

    /**
     * 验证码登录
     * @param loginForm
     * @return
     */
    @PostMapping("/loginWithCode")
    public Result<String> loginWithCode(@RequestBody LoginFormDTO loginForm) {
//        String ip = IpAddressUtil.getIpAddr(request);
//        IpAddressUtil.getIpPossessionByFile(ip);
        String token = userService.loginWithCode(loginForm);
        return Result.success(token);
    }


    /**
     * 密码登录
     * @param loginForm
     * @return
     */
    @PostMapping("/loginWithPassword")
    public Result<String> loginWithPassword(@RequestBody LoginFormDTO loginForm) {
        String token = userService.loginWithPassword(loginForm);
        return Result.success(token);
    }


    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        // 1. 获取请求头中的 token
        String token = request.getHeader("Authorization");
        // 2. 调用 UserService 处理登出逻辑
        userService.logout(token);
        // 3. 返回成功信息
        return Result.success(LOGOUT_SUCCESS);
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<UserDTO> getUserInfo() {
        UserDTO userInfo = userService.getUserInfo();
        return Result.success(userInfo);
    }


    /**
     * 根据用户ID查询用户信息
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public Result<UserDTO> getUserById(@PathVariable("id") String userId) {
        UserDTO user = userService.getUserById(userId);
        return Result.success(user);
    }


    /**
     * 修改用户信息
     * @param userDTO 要更新的用户信息
     * @return 更新结果
     */
    @PutMapping
    public Result updateUser(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }

    /**
     * 修改密码
     * @param password 新密码
     * @param confirmPassword 确认密码
     * @return 修改结果
     */
    @PutMapping("/changePassword")
    public Result changePassword(@RequestParam("password") String password,
                                 @RequestParam("confirmPassword") String confirmPassword) {
        userService.changePassword(password, confirmPassword);
        return Result.success(MessageConstant.MODIFY_SUCCESS);
    }

}